package com.school.studentServlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.school.studentServlet.cmd.StudentCommand;
import com.school.studentServlet.cmd.StudentHomeCommand;
import com.school.studentServlet.cmd.ViewExamDateSheetCommand;
import com.school.studentServlet.cmd.ViewStudentAssignmentCommand;
import com.school.studentServlet.cmd.ViewStudentAttendanceCommand;
import com.school.studentServlet.cmd.ViewStudentFeesDetailsCommand;
import com.school.studentServlet.cmd.ViewStudentProfileCommand;
import com.school.studentServlet.cmd.ViewStudentReportCardCommand;
import com.school.studentServlet.cmd.ViewStudentTimeTableCommand;



/**
 * This servlet will act as student controller which will accept request from and send response to student only.
 * Servlet implementation class StudentController
 * @author KK
 */

	public class StudentController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(StudentController.class.getSimpleName());
	   
	private ServletConfig config;
	private Map<String, StudentCommand> studentCommands;
	
	/**
	 * 
	 */
	public void init(ServletConfig config) {
		log.info("Entered init(ServletConfig)");
		this.config = config;
		log.info("Cached the ServletConfig object");
		
		studentCommands = new HashMap<String, StudentCommand>();
		studentCommands.put("studentHome", new StudentHomeCommand());
		studentCommands.put("viewStudProfile", new ViewStudentProfileCommand());
		studentCommands.put("viewStudTimeTable", new ViewStudentTimeTableCommand());
		studentCommands.put("viewStudAttendance", new ViewStudentAttendanceCommand());
		studentCommands.put("viewStudAssignment", new ViewStudentAssignmentCommand());
		studentCommands.put("viewStudReportCard", new ViewStudentReportCardCommand());
		studentCommands.put("viewStudFeesDetails", new ViewStudentFeesDetailsCommand());
		studentCommands.put("viewExamDateSheet", new ViewExamDateSheetCommand());
		
		
		

		config.getServletContext().setAttribute("studentCommands", studentCommands);
		log.info("Exited init(ServletConfig)");
		
	}
	
	/**
	 * 
	 */
	public void init() {
		log.info("Entered init()");
		log.info("Exited init()");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}
	
	private void processRequest(HttpServletRequest req, HttpServletResponse resp) 
	throws ServletException, IOException {
		HttpSession session = req.getSession();
		String studentCommand = req.getParameter("cmd");
		
		System.out.println("Command received is " + studentCommand);
		
		if (studentCommands.get(studentCommand) == null) {
			studentCommand = "studentHome";
		}
		
		if(session.getAttribute("type")== "student"){
			studentCommands.get(studentCommand).processRequest(req, resp, config);
		}
		else {
			System.out.println("Acting very smart?? :-):-)!! Relogin to continue!");
		}
	}

}
